package BanyuanClub.Study.Day0308.多线程.习题一;

public class Text2 {
    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5,6,7,8,9};
        class sumRun implements Runnable{
            int sum = 0;
            boolean b;

            public sumRun(boolean b) {
                this.b = b;
            }

            public int getSum() {
                return sum;
            }


            @Override
            public void run() {
                for (int i = this.b == true ? 0 : 1; i < arr.length; i+=2) {
                    sum += arr[i];
                    System.out.println(Thread.currentThread() + "--" + i) ;
                }
            }
        }

        sumRun sm1 = new sumRun(true);
        sumRun sm2 = new sumRun(false);
        Thread th1 = new Thread(sm1);
        Thread th2 = new Thread(sm2);
        th1.start();
        th2.start();
        try {
            th1.join();
            th2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(sm1.getSum() + sm2.getSum());
    }
}
